<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../lib/sugar.html">
<link rel="import" href="../model/cq-graph.html">
<link rel="import" href="../model/cqstats-graph-data.html">
<link rel="import" href="../model/cqstats-ratio-graph-data.html">
<link rel="import" href="../model/json-graph-data.html">
<link rel="import" href="./tom-cq-graph.html">

<polymer-element name="tom-cq-graphs" attributes="project">
  <template>
    <h1>Commit Queue Status</h1>
    Click on graph points for detailed information.
    <template repeat="{{ graph in graphList }}">
      <tom-cq-graph graph="{{ graph }}"></tom-cq-graph>
    </template>
  </template>
  <script>
    Polymer({
      ready: function() {
        this.graphList = [
          this._falseRejectGraph(),
          this._timeGraphs(),
          this._queueLengthGraph(),
        ].flatten();
      },
      _timeGraphs: function() {
        var intervalMinutes = 60;
        var dataPointCount = 25;
        var unit = 'minutes';
        var varying = [{
          heading: 'Times: Single Run',
          statName: 'attempt-durations',
          alerts: {
            p90: 120,
            mean: 60,
          },
          itemAlertThreshold: 60,
        }, {
          heading: 'Times: Time in Queue Over All Runs',
          statName: 'patchset-total-commit-queue-durations',
        }, {
          heading: 'Times: Time from Checking "Commit" Box to Complete',
          statName: 'patchset-total-wall-time-durations',
        }];
        var names = varying.map(function(item) { return item.statName });
        var dataList = CQStatsGraphData.createBatch(
            this.project, intervalMinutes, names, dataPointCount);
        return dataList.map(function(data, i) {
          var graph = varying[i];
          return new CQGraph(graph.heading, unit, unit, data, graph.alerts,
              graph.itemAlertThreshold);
        });
      },
      _falseRejectGraph: function() {
        var intervalMinutes = 60 * 24;
        var dataPointCount = 30;
        var data = new CQStatsRatioGraphData('false reject rate', this.project,
            intervalMinutes, 'attempt-false-reject-count', 'attempt-count',
            dataPointCount);
        var alerts = { 'false reject rate': 1 };
        var itemAlertThreshold = 2;
        return new CQGraph('False CQ Attempt Rejection Rate',
            'percent', 'false rejects', data, alerts, itemAlertThreshold);
      },
      _queueLengthGraph: function() {
        var url = '/project/' + this.project + '/cq-length';
        return new CQGraph('Commit Queue Length', 'count', '',
            new JSONGraphData(url));
      },
    });
  </script>
</polymer-element>
